package com.example.demo6.ui.lose;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.recyclerview.widget.RecyclerView;

import com.example.demo6.R;
import com.example.demo6.SQLite.LostGoods;

import java.util.List;
public class LostAdapter extends RecyclerView.Adapter<LostAdapter.ViewHolder> {
        private List<LostGoods> mData;
        private LayoutInflater layoutInflater;
        Context context;

        public LostAdapter(Context context, List<LostGoods> mData) {
            this.context=context;
            layoutInflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            this.mData = mData;
        }

        public  class ViewHolder extends RecyclerView.ViewHolder {
            public TextView ShowLostID;
            public TextView ShowLostTime;

            public TextView ShowLostItem;
            public TextView ShowLostRegion;

            public ViewHolder(View convertView) {
                super(convertView);
                ShowLostID=convertView.findViewById(R.id.ShowLostID);
                ShowLostTime=convertView.findViewById(R.id.ShowLostTime);
                ShowLostItem = convertView.findViewById(R.id.ShowLostItem);
                ShowLostRegion =convertView.findViewById(R.id.ShowLostRegion);
            }
        }
        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View v= layoutInflater.inflate(R.layout.lost_list_item,parent, false);
            return new ViewHolder(v);
        }
        @Override
        public void onBindViewHolder(@NonNull ViewHolder holder, @SuppressLint("RecyclerView") final int position) {
            //使用Room，有变化
//            holder.ShowLostID.setText(mData.get(position).goodsId);
            holder.ShowLostID.setText(Integer.toString(mData.get(position).goodsId));
            holder.ShowLostTime.setText(mData.get(position).lostTime);
            holder.ShowLostItem.setText(mData.get(position).lostGoods);
            holder.ShowLostRegion.setText(mData.get(position).lostPlace);

            //导航到detailfragment
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    NavController navController= Navigation.findNavController((Activity) context,R.id.nav_host_fragment_activity_main);
                    Bundle bundle=new Bundle();
                    bundle.putInt("goodsId",mData.get(position).goodsId);
                    navController.navigate(R.id.losedetailFragment,bundle);
                }
            });
        }
        @Override
        public int getItemCount() {
            return mData.size();
        }
    }
